home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4000 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: access1.digex.net!not-for-mail
  2. From: ell@access1.digex.net (Ell)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Copy constructing an already default constructed object
  5. Date: 27 Jan 1996 04:41:20 GMT
  6. Organization: The Universe
  7. Message-ID: <4ecadg$5t5@news4.digex.net>
  8. References: <4e906b$stk@elaine32.Stanford.EDU> <4eal0n$hgq@dawn.mmm.com> <3108ef14.340699@nntp> <4ebehj$5i7@news.bridge.net>
  9. NNTP-Posting-Host: access1.digex.net
  10. X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
  11.  
  12. David Byrden (100101.2547@compuserve.com) wrote:
  13. : >>  A::operator =(const A& other)
  14. : >>  {
  15. : >>    b0 = other.b0 ;
  16. : >>    b1 = other.b1;
  17. : >>  }
  18. : >> To me this seems like a major pain in the butt.
  19. : That's precisely why the compiler will automatically generate a public 
  20. : assignment operator with this behaviour, if you don't bother to write 
  21. : one.
  22.  
  23. Right, but the problem with a default op= is when the object to be copied
  24. has data members that are pointers you don't want copied to the copied to
  25. object.  A default op= will copy the pointer address into the copied to
  26. object (deep copy), but in most cases all you want in the copied to object
  27. is the same values in the copied object (shallow copy).  Defining your own
  28. op= allows you to copy the values of the copied object without copying its
  29. pointer addresses.  See Meyers' Effective C++ book and the comp.lang.c++
  30. FAQ for more on this. 
  31.  
  32. : >>  Do you have to overload = for all the contained operators too?
  33. : >>  All I really want to do is have the copy constructor invoked on the 
  34. : >> piece that piece of memory.  Why can't this be done?
  35.  
  36. A copy ctor is generally for creating new objects from existing ones.  op=
  37. is generally for copying an existing object's data values into another
  38. existing object.  While the two ops often interact, they have separate 
  39. primary puposes.  Best to write both functions when your objects 
  40. contain pointers because you'll generally get the wrong kind by default.  
  41. See the above mentioned books on this point also.
  42.  
  43. : Brien, I genuinely cannot understand what you are trying to say in these 
  44. : two sentences. Sloppy English, like C++, can be a "major pain the the 
  45. : butt".
  46.  
  47. Sometimes people simply need to learn more C++, to phrase a question 
  48. properly.  The writer did pretty well by me for both questions.
  49.  
  50. Elliott
  51.